package com.euvmp.sdk;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

import java.util.Base64;

/**
 * @Description api加解密相关
 * @Author YangZheng 328170112@qq.com
 * @Date 2020-06-23 11:48
 */
public class EncryptionHelper {
    private final SymmetricCrypto sm4;

    public EncryptionHelper(String key) {
        byte[] bytes = Base64.getDecoder().decode(key + "==");
        sm4 = SmUtil.sm4(bytes);
    }

    /**
     * 处理解密,校验相关,返回请求参数
     * @author YangZheng 328170112@qq.com
     * @date 2020-06-23 14:44
     */
    public String decode(String data){
        return sm4.decryptStr(data, CharsetUtil.CHARSET_UTF_8);
    }
    /**
     * 对返回的对象进行加密
     * @author YangZheng 328170112@qq.com
     * @date 2020-06-23 14:50
     */
    public String encode(String data){
        return sm4.encryptHex(data);
    }
}
